/**
 * Mar 19, 2014 1:56:30 PM
 */
package elitech.vietnam.weatherfashion.tabs.settings;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import elitech.vietnam.weatherfashion.MainActivity;
import elitech.vietnam.weatherfashion.R;
import elitech.vietnam.weatherfashion.WFApplication;
import elitech.vietnam.weatherfashion.config.Const;
import elitech.vietnam.weatherfashion.config.GlobalValue;
import elitech.vietnam.weatherfashion.dto.MemberDTO;
import elitech.vietnam.weatherfashion.tabs.AbstractFragment;
import elitech.vietnam.weatherfashion.ultilities.Utilities;
import elitech.vietnam.weatherfashion.ultilities.ValueOption;
import elitech.vietnam.weatherfashion.widget.NotifyButton;

/**
 * @author Cong
 *
 */
public class LanguageSettingsFragment extends AbstractFragment implements OnCheckedChangeListener, OnClickListener {

	RadioGroup mRGroup;
	RadioButton[] mRadio = new RadioButton[4];
	Button mButtonBack;
	ValueOption option;
	NotifyButton button_notification;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.settings_language, container, false);
		setupView(view);
		
		mRGroup = (RadioGroup) view.findViewById(R.id.layout_main);
		mRadio[0] = (RadioButton) view.findViewById(R.id.stlang_auto);
		mRadio[1] = (RadioButton) view.findViewById(R.id.stlang_korea);
		mRadio[2] = (RadioButton) view.findViewById(R.id.stlang_eng);
		mRadio[3] = (RadioButton) view.findViewById(R.id.stlang_vi);
		button_notification = (NotifyButton) view.findViewById(R.id.button_notification);
		
		((MainActivity) getActivity()).setCurrentNotifyButton(button_notification);

		option = GlobalValue.getInstance(getActivity()).readOptionInfo();

		MemberDTO m = GlobalValue.getInstance(getActivity()).readUserInfo();
		if (m == null)
			button_notification.setVisibility(View.GONE);
		else 
			button_notification.setVisibility(View.VISIBLE);
		//button_notification.setOnClickListener(this);

		mRadio[option.getLanguageIndex()].setChecked(true);
		mRGroup.setOnCheckedChangeListener(this);
		
		return view;
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.stnoti_button_back:
			getActivity().onBackPressed();
			break;
		case R.id.button_notification:
			Log.w("Notification", "Num Count: " + WFApplication.getInstance().mListNoti.size());
			((MainActivity) getActivity()).mNotiWindow.showAsDropDown(button_notification);
			((MainActivity) getActivity()).mNotiWindow.notifyDataChanged();
			button_notification.setNumber(0);
			break;
		default:
			break;
		}
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		String code = "";
		switch (checkedId) {
		case R.id.stlang_auto:
			code = Utilities.convertCountryCodeToLanguageCode(Const.COUNTRY_CODE);
			option.setLanguageIndex(0);
			break;
		case R.id.stlang_korea:
			code = "ko";
			option.setLanguageIndex(1);
			break;
		case R.id.stlang_eng:
			code = "en";
			option.setLanguageIndex(2);
			break;
		case R.id.stlang_vi:
			code = "vi";
			option.setLanguageIndex(3);
			break;

		default:
			break;
		}
		option.setLanguageCode(code);
		GlobalValue.getInstance(getActivity()).writeOptionInfo(option);
		
		Intent intent = getActivity().getIntent();
		intent.putExtra(SettingsFragment.SETTING_RELOAD, true);
		getActivity().finish();
		startActivity(intent);
	}
}
